<template>
  <el-dialog
    :title="!dataForm.signId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="团购表id" prop="groupbuyId">
      <el-input v-model="dataForm.groupbuyId" placeholder="团购表id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="用户电话" prop="userTel">
      <el-input v-model="dataForm.userTel" placeholder="用户电话"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          signId: 0,
          groupbuyId: '',
          userId: '',
          userName: '',
          userTel: ''
        },
        dataRule: {
          groupbuyId: [
            { required: true, message: '团购表id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          userTel: [
            { required: true, message: '用户电话不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.signId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.signId) {
            this.$http({
              url: this.$http.adornUrl(`/consumer/sign/info/${this.dataForm.signId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.groupbuyId = data.sign.groupbuyId
                this.dataForm.userId = data.sign.userId
                this.dataForm.userName = data.sign.userName
                this.dataForm.userTel = data.sign.userTel
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/consumer/sign/${!this.dataForm.signId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'signId': this.dataForm.signId || undefined,
                'groupbuyId': this.dataForm.groupbuyId,
                'userId': this.dataForm.userId,
                'userName': this.dataForm.userName,
                'userTel': this.dataForm.userTel
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
